<?php
/**
 * Shop_Model_AttrValue
 * 
 * @version $Id$
 * @author $Author$
 */
class Shop_Model_AttrValue extends Promotor_Model_Scaffold {

	protected $_dbTableClass = 'Shop_Model_DbTable_AttrValue';
	
	/**
	 * @param integer $productId
	 * @param array $attribsValues
	 * @return void
	 */
	public function attacheAttribsValuesToProduct($productId, array $attribsValues) {
		$table = $this->getDbTable();
		$db = $table->getAdapter();
		
		$db->beginTransaction();
		
		$table->delete($db->quoteInto('product_id = ?', $productId));

		try {
			foreach ($attribsValues as $attrId => $attrValue)
			{
				if (!is_numeric($attrId) || is_null($attrValue))
				{
					continue;
				}
				
				$data = array(
					'attr_id' => $attrId,
					'product_id' => $productId,
					'value' => $attrValue
				);

				$table->insert($data);
			}

			$db->commit();
			$this->_setStatus(self::SUCCESS);
		} catch (Zend_Db_Exception $e) {
			$db->rollBack();
			$this->_addException($e);
			$this->_setStatus(self::FAILURE);
		}
	}
}